package com.qls.web.modules.ord.service.impl;

import com.qls.core.dto.HtDailyRoom;
import com.qls.core.entity.OrdOrder;
import com.qls.core.utils.DateUtils;
import com.qls.core.utils.R;
import com.qls.redis.RedisUtil;
import com.qls.web.modules.ht.service.HtLogicRoomService;
import com.qls.web.modules.ord.entity.DeliGood;
import com.qls.web.modules.ord.service.OrdGuestService;
import com.qls.web.modules.ord.service.OrdOrderService;
import com.qls.web.modules.ord.service.OrderDealService;
import com.qls.web.modules.ord.service.OrderFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author doyoung
 */
@Service
@Slf4j
public class OrderDealServiceImpl implements OrderDealService {

    @Autowired
    private OrdOrderService ordOrderService;

    @Autowired
    private OrdGuestService ordGuestService;
    @Autowired
    private OrderFeignService orderFeignService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private HtLogicRoomService htLogicRoomService;

    @Override
    public List<Map<String, Object>> getRatesList(Integer id) {
        OrdOrder ordOrderEntity = ordOrderService.getById(id);
        List<Map<String, Object>> list = new ArrayList<>();
        Date startDate = ordOrderEntity.getCheckInDate();
        Date endDate = ordOrderEntity.getCheckOutDate();
        List<HtDailyRoom> htDailyRooms = redisUtil.getRatesBatch(DateUtils.getBetweenDays(DateUtils.format(startDate), DateUtils.format(endDate)), ordOrderEntity.getRateplanCode().toString());
        int buyPrice = 0;
        for (HtDailyRoom htDailyRoom : htDailyRooms) {
            buyPrice += (htDailyRoom.getPrice() - htDailyRoom.getSubPrice());
        }
        Map<String, Object> map = new HashMap<>();
        String name = htLogicRoomService.getById(ordOrderEntity.getRateplanCode()).getRpName();
        // 客人支付价
        map.put("payPrice", ordOrderEntity.getPayPrice() * 0.92);
        // 渠道下单价
        map.put("buyPrice", buyPrice);
        map.put("name", name);
        list.add(map);
        return list;
    }

    @Override
    @Transactional
    public R getLock(Integer userId, Long id, String username) {
        // 人工订单跟单条件   跟单人为null 直连订单跟单条件   跟单人为null 采购状态失败
        OrdOrder ordOrderEntity = ordOrderService.getById(id);
        Integer dealOrderPeo = ordOrderEntity.getDealOrderPeo();
        Integer buyStatus = ordOrderEntity.getBuyStatus();
        Integer orderType = ordOrderEntity.getOrderType();
        // 人工订单
        if (orderType == 0) {
            // 已跟单
            if (dealOrderPeo != null) {
                return R.error(-1, "订单已受理");
            } else {
                ordOrderEntity.setUsername(username);
                // 设置采购状态为已受理
                ordOrderEntity.setBuyStatus(1);
            }
        } else {  // 直连订单
            // 直连订单当没有跟单人和采购失败时候才能跟单处理
            if (dealOrderPeo == null || buyStatus == -1) {
                // 设置采购状态为已受理
                ordOrderEntity.setBuyStatus(1);
            } else {
                ordOrderEntity.setUsername(username);
                return R.error(-1, "订单已受理");
            }
        }
        ordOrderEntity.setDealOrderPeo(userId);
        ordOrderService.updateById(ordOrderEntity);
        return R.ok();
    }

    @Override
    @Transactional
    public R buyOther(Long id, Long userId, Integer optType, Integer status, Integer price, String orderCodeOther) {
        OrdOrder orderEntity = ordOrderService.getById(id);
        Long orderCode = orderEntity.getOrderCode();
        Long ordDealPeo = Long.valueOf(orderEntity.getDealOrderPeo().toString());
        if (!ordDealPeo.equals(userId)) {
            return R.error(-1, "跟单人不符");
        }
        // 下单
        DeliGood deliGood = new DeliGood();
        deliGood.setOptType(optType);
        deliGood.setOrderCode(orderCode);
        R r = orderFeignService.manudeligood(deliGood);
        int code = (int) r.get("code");
        if (code != 0) {
            String message = (String) r.get("msg");
            return R.error(message);
        }

        // 更改数据库价格，添加渠道订单号
        OrdOrder ordOrderEntity = new OrdOrder();

        ordOrderEntity.setId(id);
        ordOrderEntity.setBuyStatus(3);
        ordOrderEntity.setBuyOrderPrice(price);
        ordOrderEntity.setBuyOrderId(orderCodeOther);
        ordOrderEntity.setBuySource(status); // 自采订单
        ordOrderService.updateById(ordOrderEntity);
        return R.ok();
    }
}
